#!/bin/bash
# 数据库配置
# author    : guyadong
# date      : 2019/07/30
# 导入标志,确保只会被导入一次
database_config_included=true

# 脚本初始化
[ "$common_init_included" != 'true' ] && . ./common_init.sh
dbcfg_title='数据库配置'

service_online mysql $DEFAULT_DATABASE_PORT $database_jdbc_port
# 选择是否使用本地 mysql 的对话框 
# $1 对话框标题
# $2 默认选项 0 OR 1
function _database_use_local(){
    local title="$dbcfg_title"
    [ -n "$1" ] && title="$1" 
    local defaultno=    
    [ "$2" = 1 ] && defaultno='--defaultno'

    dialog  --backtitle "$DEFAULT_BACK_TITLE$context_backtitle_suffix" $DLG_YESNO_LABEL \
            --title "$title" $DLG_BACK_BTN $defaultno \
            --yesno "是否使用本地 mysql 数据库?" 5 30
}

function fun_facelog_db_use_local(){
    _database_use_local "facelog 数据库配置" $database_use_local
    local code=$?
    [ $code -lt 2 ] && database_use_local=$code
    return $code
}

function fun_fdevmgr_db_use_local(){
    _database_use_local "fdevmgr 数据库配置" $fdevmgr_db_use_local
    local code=$?
    [ $code -lt 2 ] && fdevmgr_db_use_local=$code
    return $code
}

# 检查连接参数(password 除外)是否有无效值,有则返回-1,无则返回0
# $1 $host
# $2 $port
# $3 $username
# $4 $password
# $5 $schema
function _check_db_params(){

    [[ ! "$1" =~ ^[A-Za-z0-9_\.]+$ ]] && error_message "主机名(host)无效" && return 255
    [[ ! "$2" =~ ^[0-9]{1,5}$      ]] && error_message "端口号(port)不可为空,必须为数字" && return 255
    [[ ! "$3" =~ ^[A-Za-z0-9_]+$   ]] && error_message "用户名(user)无效" && return 255
    [[ ! "$5" =~ ^[A-Za-z0-9_]+$   ]] && error_message "数据库名(schema)无效" && return 255
    return 0
}

# 输出map中的连接参数值
# $1 包含连接参数的map变量名
function _output_db_params(){
    local var=$(declare -p "$1") || exit_on_error
    eval "declare -A local params"=${var#*=}

    for key in DB_HOST DB_PORT DB_USERNAME DB_PASSWORD DB_SCHEMA; do
        echo ${params[$key]}=${!params[$key]}
    done
}
# 设置数据库连接参数
# 如果参数无效则一直循环,如果点返回则跳出循环返回3,否则返回0
# $1 包含连接参数的map变量名
# $2 对话框标题
function fun_database_connect(){
    # 保存对话框选项的值到$values,如果对话框选项返回,则$values值为'EXIT_$?'
    [ -z "$1" ] && error_message "MISS connect arguments" && exit 255
    local var=$(declare -p "$1") || exit_on_error
    eval "declare -A local params"=${var#*=}

    for key in DB_HOST DB_PORT DB_USERNAME DB_PASSWORD DB_SCHEMA; do
        [ -z "${params[$key]}" ] && error_message "MISS connect argument $key" && exit 255
    done
    local title="配置数据库连接参数"
    [ -n "$2" ] && title="$2"
    local host="${!params[DB_HOST]}"
    local port="${!params[DB_PORT]}"
    local username="${!params[DB_USERNAME]}"
    local password="${!params[DB_PASSWORD]}"
    local schema="${!params[DB_SCHEMA]}"

    while true ;
    do 
        local values=$(dialog \
            --backtitle "$DEFAULT_BACK_TITLE$context_backtitle_suffix" \
            --title "$title" $DLG_BACK_BTN --nocancel \
            --form "请输入数据库连接的参数:" 15 60 0 \
                "主机名(host):"     1 1	    "$host" 	    1 20 34 0 \
                "端口号(port):"     2 1     "$port"  	    2 20 34 0 \
                "用户名(user):"     3 1	    "$username"  	3 20 34 0 \
                "密码(password):"   4 1	    "$password" 	4 20 34 0 \
                "数据库名(schema):"  5 1    "$schema"        5 20 34 0 \
                3>&1 1>&2 2>&3 || echo EXIT_$?)
        
        [[ "$values" =~ EXIT_[0-9] ]] && return ${values##*EXIT_}
        host=$(echo     "$values" | sed -n 1p)
        port=$(echo     "$values" | sed -n 2p)
        username=$(echo "$values" | sed -n 3p)
        password=$(echo "$values" | sed -n 4p)
        schema=$(echo   "$values" | sed -n 5p)
        
        ! _check_db_params "$host" "$port" "$username" "$password" "$schema" && continue 

        # 按确认按钮时将对话框返回值分别保存到对应的变量中
        eval ${params[DB_HOST]}="$host"
        eval ${params[DB_PORT]}="$port"
        eval ${params[DB_USERNAME]}="$username"
        eval ${params[DB_PASSWORD]}="$password"
        eval ${params[DB_SCHEMA]}="$schema"
        context_mysql_configured=0
        context_mysql_host="$host"
        context_mysql_port="$port"
        context_mysql_username="$username"
        context_mysql_password="$password"
        context_mysql_schema="$schema"
        return 0

    done
}
# facelog 数据库连接参数配置
# 配置成功返回0,否则返回1
function fun_facelog_db_config(){
    declare -A local params

    params[DB_HOST]=database_jdbc_host
    params[DB_PORT]=database_jdbc_port
    params[DB_USERNAME]=database_jdbc_username
    params[DB_PASSWORD]=database_jdbc_password
    params[DB_SCHEMA]=database_jdbc_schema

    fun_database_connect params "facelog 数据库连接参数"
    local code=$?
    #_output_db_params params
    return $code
}
function fun_facelog_remote_db_config(){
    if [ $context_mysql_configured -eq 0 ] && [ "$context_mysql_host" != $DEFAULT_DATABASE_HOST ]
    then
        [ "$database_jdbc_host" = $DEFAULT_DATABASE_HOST ] && database_jdbc_host=$context_mysql_host
        [ -z "$database_jdbc_port"     ] && database_jdbc_port=$context_mysql_port
        [ -z "$database_jdbc_username" ] && database_jdbc_username="$context_mysql_username"
        [ -z "$database_jdbc_password" ] && database_jdbc_password="$context_mysql_password"
        [ -z "$database_jdbc_schema"   ] && database_jdbc_schema=$DEFAULT_DATABASE_SCHEMA
    else
        [ "$database_jdbc_host" = $DEFAULT_DATABASE_HOST ] && database_jdbc_host=
        [ -z "$database_jdbc_port"     ] && database_jdbc_port=$DEFAULT_DATABASE_PORT
        [ -z "$database_jdbc_username" ] && database_jdbc_username=$DEFAULT_DATABASE_USERNAME
        [ -z "$database_jdbc_password" ] && database_jdbc_password=$DEFAULT_DATABASE_PASSWORD
        [ -z "$database_jdbc_schema"   ] && database_jdbc_schema=$DEFAULT_DATABASE_SCHEMA
    fi

    fun_facelog_db_config
}
function fun_facelog_local_db_config(){
    database_jdbc_host=$DEFAULT_DATABASE_HOST
    if [ $context_mysql_configured -eq 0 ] && [ "$context_mysql_host" = $DEFAULT_DATABASE_HOST ]
    then
        [ -z "$database_jdbc_port"     ] && database_jdbc_port=$context_mysql_port
        [ -z "$database_jdbc_username" ] && database_jdbc_username="$context_mysql_username"
        [ -z "$database_jdbc_password" ] && database_jdbc_password="$context_mysql_password"
        [ -z "$database_jdbc_schema"   ] && database_jdbc_schema=$DEFAULT_DATABASE_SCHEMA
    else
        [ -z "$database_jdbc_port"     ] && database_jdbc_port=$DEFAULT_DATABASE_PORT
        [ -z "$database_jdbc_username" ] && database_jdbc_username=$DEFAULT_DATABASE_USERNAME
        # 没有指定密码时生成随机密码
        [ -z "$database_jdbc_password" ] && [ "$database_jdbc_username" = 'root' ] && database_jdbc_password="$(date +%s | md5sum | base64 | head -c 8)"
        [ -z "$database_jdbc_password" ] && database_jdbc_password=$DEFAULT_DATABASE_PASSWORD
        [ -z "$database_jdbc_schema"   ] && database_jdbc_schema=$DEFAULT_DATABASE_SCHEMA
    fi
    
    fun_facelog_db_config
}

# fdevmgr 数据库连接参数配置
# 配置成功返回0,否则返回1
function fun_fdevmgr_db_config(){
    declare -A local params

    params[DB_HOST]=fdevmgr_db_host
    params[DB_PORT]=fdevmgr_db_port
    params[DB_USERNAME]=fdevmgr_db_username
    params[DB_PASSWORD]=fdevmgr_db_password
    params[DB_SCHEMA]=fdevmgr_db_schema

    fun_database_connect params "fdevmgr 数据库连接参数"
    local code=$?
    #_output_db_params params
    return $code
}

function fun_fdevmgr_remote_db_config(){
    if [ $context_mysql_configured -eq 0 ] && [ "$context_mysql_host" != $DEFAULT_DATABASE_HOST ]
    then
        [ "$fdevmgr_db_host" = $DEFAULT_DATABASE_HOST ] && fdevmgr_db_host=$context_mysql_host
        [ -z "$fdevmgr_db_port"     ] && fdevmgr_db_port=$context_mysql_port
        [ -z "$fdevmgr_db_username" ] && fdevmgr_db_username="$context_mysql_username"
        [ -z "$fdevmgr_db_password" ] && fdevmgr_db_password="$context_mysql_password"
        [ -z "$fdevmgr_db_schema"   ] && fdevmgr_db_schema=$DEFAULT_DATABASE_SCHEMA
    else
        [ "$fdevmgr_db_host" = $DEFAULT_DATABASE_HOST ] && fdevmgr_db_host=
        [ -z "$fdevmgr_db_port"     ] && fdevmgr_db_port=$DEFAULT_DATABASE_PORT
        [ -z "$fdevmgr_db_username" ] && fdevmgr_db_username=$DEFAULT_DATABASE_USERNAME
        [ -z "$fdevmgr_db_password" ] && fdevmgr_db_password=$DEFAULT_DATABASE_PASSWORD
        [ -z "$fdevmgr_db_schema"   ] && fdevmgr_db_schema=$DEFAULT_DATABASE_SCHEMA
    fi
    fun_fdevmgr_db_config
}

function fun_fdevmgr_local_db_config(){ 
    fdevmgr_db_host=$DEFAULT_DATABASE_HOST
    if [ $context_mysql_configured -eq 0 ] && [ "$context_mysql_host" = $DEFAULT_DATABASE_HOST ]
    then
        [ -z "$fdevmgr_db_port"     ] && fdevmgr_db_port=$context_mysql_port
        [ -z "$fdevmgr_db_username" ] && fdevmgr_db_username="$context_mysql_username"
        [ -z "$fdevmgr_db_password" ] && fdevmgr_db_password="$context_mysql_password"
        [ -z "$fdevmgr_db_schema"   ] && fdevmgr_db_schema=$DEFAULT_DATABASE_SCHEMA
    else
        [ -z "$fdevmgr_db_port"     ] && fdevmgr_db_port=$DEFAULT_DATABASE_PORT
        [ -z "$fdevmgr_db_username" ] && fdevmgr_db_username=$DEFAULT_DATABASE_USERNAME
        # 没有指定密码时生成随机密码
        [ -z "$fdevmgr_db_password" ] && [ "$fdevmgr_db_username" = 'root' ] && fdevmgr_db_password="$(date +%s | md5sum | base64 | head -c 8)"
        [ -z "$fdevmgr_db_password" ] && fdevmgr_db_password=$DEFAULT_DATABASE_PASSWORD
        [ -z "$fdevmgr_db_schema"   ] && fdevmgr_db_schema=$DEFAULT_DATABASE_SCHEMA
    fi
    
    fun_fdevmgr_db_config
}

function fun_reset_facelog_tables(){
    local title="$dbcfg_title"
    local defaultno=    
    [ "$database_reset_tables" = 1 ] && defaultno='--defaultno'

    dialog  --backtitle "$DEFAULT_BACK_TITLE$context_backtitle_suffix" $DLG_YESNO_LABEL \
            --title "$title" $DLG_BACK_BTN $defaultno \
            --yesno "是否重建 facelog 表?\n选择‘是’将删除所有表数据" 0 0 
    local code=$?
    [ $code -lt 2 ] && database_reset_tables=$code
    return $code
}
function fun_reset_fdevmgr_tables(){
    local title="$dbcfg_title"
    local defaultno=    
    [ "$fdevmgr_db_reset_tables" = 1 ] && defaultno='--defaultno'

    dialog  --backtitle "$DEFAULT_BACK_TITLE$context_backtitle_suffix" $DLG_YESNO_LABEL \
            --title "$title" $DLG_BACK_BTN $defaultno \
            --yesno "是否重建 fdevmgr 表?\n选择‘是’将删除所有表数据" 0 0 
    local code=$?
    [ $code -lt 2 ] && fdevmgr_db_reset_tables=$code
    return $code
}
# 返回由 database_jdbc_host database_jdbc_port database_jdbc_schema 参数生成的JDBC URL
function facelog_jdbc_url(){
    echo "jdbc:mysql://${database_jdbc_host}:${database_jdbc_port}/${database_jdbc_schema}?characterEncoding=utf8"
}
# 返回由 fdevmgr_db_host fdevmgr_db_port fdevmgr_db_schema 参数组成的 JDBC URL
function fdevmgr_jdbc_url(){
    echo "jdbc:mysql://${fdevmgr_db_host}:${fdevmgr_db_port}/${fdevmgr_db_schema}?serverTimezone=GMT%2B8&serverTimezone=UTC&&useUnicode=true&&characterEncoding=utf8"
}



#fun_facelog_db_config
#fun_fdevmgr_db_config
